﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Drawing;
using System.IO;
using System.Xml.Linq;
using SharingCenter.Web.Classes.enums;

namespace SharingCenter.Web.Classes
{
    public static class WebConsts
    {
        // server consts
        public static readonly TimeSpan WEBSERVER_OFFSET = new TimeSpan(0, 0, -14);

        // db consts
        public static readonly bool IS_LOCAL = HttpContext.Current.Request.Url.Host.ToLower().Contains("localhost");
        public static readonly string SERVER_DB_NAME = ConfigurationManager.AppSettings["ServerDBName"];
        public static readonly string LOCAL_DB_NAME = ConfigurationManager.AppSettings["LocalDBName"];

        // website consts
        public static readonly string AVATAR_IMG_FORMAT = ConfigurationManager.AppSettings["AvatarImageFileFormat"];
        public static readonly string AVATAR_TEMP_FOLDER = ConfigurationManager.AppSettings["AvatarImageTempFolder"];
        public static readonly string AVATAR_FOLDER = ConfigurationManager.AppSettings["AvatarImageFolder"];
        public static readonly Size AVATAR_IMAGE_SIZE = new Size(int.Parse(ConfigurationManager.AppSettings["AvatarImageWidth"]),
            int.Parse(ConfigurationManager.AppSettings["AvatarImageHeight"]));
        public static readonly Size AVATAR_IMAGE_THUMB_SIZE = new Size(int.Parse(ConfigurationManager.AppSettings["AvatarImageThumbWidth"]),
            int.Parse(ConfigurationManager.AppSettings["AvatarImageThumbHeight"]));
        public static readonly Size AVATAR_IMAGE_TINY_THUMB_SIZE = new Size(int.Parse(ConfigurationManager.AppSettings["AvatarImageTinyThumbWidth"]),
            int.Parse(ConfigurationManager.AppSettings["AvatarImageTinyThumbHeight"]));

        public static readonly string AVATAR_DEFAULT_IMG_TINY_PATH = ConfigurationManager.AppSettings["DefaultAvatarTinyImagePath"];
        public static readonly string AVATAR_DEFAULT_IMG_PATH = ConfigurationManager.AppSettings["DefaultAvatarImagePath"];
        public static readonly string AVATAR_DEFAULT_IMG_THUMBNAIL_PATH = ConfigurationManager.AppSettings["DefaultAvatarThumbImagePath"];
        public static readonly string G_AVATAR_DEFAULT_IMG_PATH = ConfigurationManager.AppSettings["DefaultGroupAvatarImagePath"];
        public static readonly string G_AVATAR_DEFAULT_IMG_THUMBNAIL_PATH = ConfigurationManager.AppSettings["DefaultGroupAvatarThumbImagePath"];

        public static readonly bool ACTIVITY_ADMIN_APPROVAL_DEFAULT = true;

        // Session keys
        public static readonly string SK_MODAL_MESSAGE = "MODAL_MESSAGE_ITEM";
        public static readonly string SK_DELAUTH_TOKEN = "SS_DELAUTH_TOKEN";

        // Settings
        public static readonly bool MUTUAL_CONTACT = Convert.ToBoolean(ConfigurationManager.AppSettings["MutualContact"]);

        // Misc consts
        //public static readonly string SITE_NAME = ConfigurationManager.AppSettings["SiteName"];
        public static readonly string ERROR_MESSAGE_DEFAULT = "抱歉，网站遇到了暂时的问题，我们将尽快解决。请稍后回来。";
        public static readonly string DATE_FORMAT = "yyyy年M月d日";
        public static readonly string DATE_FORMAT_NO_YEAR = "M月d日";
        public static readonly string CON_STR = ConfigurationManager.ConnectionStrings["websiteConnectionString"].ConnectionString;

        public static readonly string ADMIN_USERNAME_PREFIX = "admin.";
        public static readonly string GENERAL_ADMIN_ROLENAME = "GeneralAdmin";
        public static readonly string SITE_ADMIN_ROLENAME = "SiteAdmin";

        public static readonly string APPDATA_DIR = "~/App_Data";
        public static readonly Guid FAKE_GUID = Guid.Empty;
        public static readonly char[] STR_SEPERATOR = { '|' };
        public static readonly char MAIL_PARAM_L_DELIMETER = '$';
        public static readonly char MAIL_PARAM_R_DELIMETER = '$';
        public static readonly string SITE_URL = string.Format("{0}://{1}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.Url.Authority);

        public static readonly string DELAUTH_COOKIE = "delauthtoken";

        

        // Request names
        public static readonly string RN_CONTACT = "CNTCT";
        public static readonly string RN_USER_EVENT = "UEVNT";
        public static readonly string RN_GROUP_EVENT = "GEVNT";

        public static readonly string POST_ICON_DIR = @"~/Content/images/threadicons";
        public static Dictionary<string, string> POST_ICON_LIST
        {
            get 
            {
                Dictionary<string, string> ret;
                if (HttpContext.Current.Application["POST_ICONS"] == null)
                {
                    ret = new Dictionary<string, string>();
                    ret.Clear();
                    XDocument xdoc = XDocument.Load(HttpContext.Current.Server.MapPath(WebConsts.APPDATA_DIR + "/PostIcons.xml"));
                    foreach (XElement el in xdoc.Root.Elements())
                    {
                        ret.Add(el.Attribute("file").Value, el.Attribute("text").Value);
                    }

                    HttpContext.Current.Application["POST_ICONS"] = ret;
                }
                else
                {
                    ret = (Dictionary<string, string>)HttpContext.Current.Application["POST_ICONS"];
                }
                return ret;
            }
        }

        public static  Dictionary<string, string> SKILL_LEVEL_LIST
        {
            get
            {
                Dictionary<string, string> ret;
                if (HttpContext.Current.Application["SKILL_LEVEL"] == null)
                {
                    ret = new Dictionary<string, string>();
                    XDocument x = XDocument.Load(HttpContext.Current.Server.MapPath(string.Format("{0}/SkillLevels.xml", WebConsts.APPDATA_DIR)));
                    foreach (XElement el in x.Root.Elements())
                    {
                        ret.Add(el.Attribute("Value").Value, el.Attribute("Name").Value);
                    }
                    HttpContext.Current.Application["SKILL_LEVEL"] = ret;
                }
                else
                {
                    ret = (Dictionary<string, string>)HttpContext.Current.Application["SKILL_LEVEL"];
                }
                return ret;
            }
        }

        public static Dictionary<string, string> DAYS_OF_WEEK
        {
            get
            {
                Dictionary<string, string> ret;
                if (HttpContext.Current.Application["DAYS_OF_WEEK_TEXT"] == null)
                {
                    ret = new Dictionary<string, string>();
                    ret.Add("7", "日");
                    ret.Add("1", "一");
                    ret.Add("2", "二");
                    ret.Add("3", "三");
                    ret.Add("4", "四");
                    ret.Add("5", "五");
                    ret.Add("6", "六");
                    HttpContext.Current.Application["DAYS_OF_WEEK_TEXT"] = ret;
                }
                else
                {
                    ret = (Dictionary<string, string>)HttpContext.Current.Application["DAYS_OF_WEEK_TEXT"];
                }
                return ret;
            }
        }

        public static Dictionary<string, string> ENTITY_NAME
        {
            get
            {
                Dictionary<string, string> ret;
                if (HttpContext.Current.Application["MESSAGE_ENTITY"] == null)
                {
                    ret = new Dictionary<string, string>();
                    ret.Add("Learning", "互助");
                    ret.Add("Contact", "好友");
                    ret.Add("Event", "活动");
                    HttpContext.Current.Application["MESSAGE_ENTITY"] = ret;
                }
                else
                {
                    ret = (Dictionary<string, string>)HttpContext.Current.Application["MESSAGE_ENTITY"];
                }
                return ret;
            }
        }

        public static Dictionary<string, string> REQUEST_STATUS
        {
            get
            {
                Dictionary<string, string> ret;
                if (HttpContext.Current.Application["REQUEST_STATUS"] == null)
                {
                    ret = new Dictionary<string, string>();
                    ret.Add("Approved", "同意");
                    ret.Add("Rejected", "拒绝");
                    ret.Add("Cancelled", "取消");
                    ret.Add("Ignored", "忽略");
                    HttpContext.Current.Application["REQUEST_STATUS"] = ret;
                }
                else
                {
                    ret = (Dictionary<string, string>)HttpContext.Current.Application["REQUEST_STATUS"];
                }
                return ret;
            }
        }

        public static Dictionary<int, string> THREAD_TYPE
        {
            get
            {
                Dictionary<int, string> ret;
                if (HttpContext.Current.Application["THREAD_TYPE"] == null)
                {
                    ret = new Dictionary<int, string>();
                    ret.Add(1, "原创");
                    ret.Add(2, "转载");
                    ret.Add(3, "贴图");
                    HttpContext.Current.Application["THREAD_TYPE"] = ret;
                }
                else
                {
                    ret = (Dictionary<int, string>)HttpContext.Current.Application["THREAD_TYPE"];
                }
                return ret;
            }
        }

        public static Dictionary<SiteEvent, string> EVENT_TEXT
        {
            get
            {
                Dictionary<SiteEvent, string> ret;
                if (HttpContext.Current.Application["EVENT_TEXT"] == null)
                {
                    ret = new Dictionary<SiteEvent, string>();
                    XDocument x = XDocument.Load(HttpContext.Current.Server.MapPath(string.Format("{0}/SiteEventText.xml", WebConsts.APPDATA_DIR)));
                    foreach (XElement el in x.Root.Elements())
                    {
                        if (el.Attribute("key").Value.Trim() != string.Empty)
                        {
                            ret.Add((SiteEvent)Enum.Parse(typeof(SiteEvent), el.Attribute("key").Value.Trim()), el.Attribute("value").Value);
                        }
                    }
                    HttpContext.Current.Application["EVENT_TEXT"] = ret;
                }
                else
                {
                    ret = (Dictionary<SiteEvent, string>)HttpContext.Current.Application["EVENT_TEXT"];
                }
                return ret;
            }
        }
        
    }
}
